package com.aaa.util;

import org.apache.http.HttpResponse;

import java.util.HashMap;
import java.util.Map;

/**
 * Demo class
 * 获取验证码工具类
 * @author 尚冠峰
 * @date 2018/12/25
 */
public class GetCodeUtil {
    public static String getCode(String phone) {
        System.out.println("------------------------------------------------------------------");
        System.out.println(phone);
        long number = (long) (Math.random() * 1000000);
        String code = String.valueOf(number);
        String host = "https://cxkjsms.market.alicloudapi.com";
        String path = "/chuangxinsms/dxjk";
        String method = "POST";
        String appcode = "255d1034e31d47888be97e095e8735c2";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        /*Map<String, String> querys = new HashMap<String, String>();
        querys.put("param", code);
        querys.put("phone", phone);
        querys.put("sign", "1");
        querys.put("skin", "1");*/
        //JDK 1.8示例代码请在这里下载：  http://code.fegine.com/Tools.zip

        Map<String, String> querys = new HashMap<String, String>();
        querys.put("content", "【易客】尊敬的用户,您的验证码是："+number+",请在页面提交此验证码完成校验!");
        querys.put("mobile", phone);
        Map<String, String> bodys = new HashMap<String, String>();


        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());
            //获取response的body
            //System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return code;

    }

}
